# Author:: Alexandre Ribeiro de Souza (<alexandre@lumis.com.br>)

tag('javaserver_tomcat6')

service "tomcat6" do
  supports :status => true, :restart => true, :reload => true
  action :enable
end

bash "Copy Lumis Portal Shared Libs" do
  code <<-EOH
  cp -rf #{node['lumis']['lumisportal']['home']}/lib/shared/* /usr/share/tomcat6/lib/
  cp -rf #{node['lumis']['lumisportal']['home']}/lib/endorsed /usr/share/tomcat6/
  EOH
end

directory "/usr/share/tomcat6/conf/Catalina/localhost" do
  mode "0755"
  recursive true
  not_if "test -e /usr/share/tomcat6/conf/Catalina/localhost"
end

template "/etc/tomcat6/tomcat6.conf" do
  source "tomcat6.conf.erb"
  owner "root"
  group "root"
  mode "0644"
  notifies :restart, resources(:service => "tomcat6")
end

shutdown_port = 8005 + (node['lumis']['lumisportal']['ports'] * 100)
http_port = 8080 + (node['lumis']['lumisportal']['ports'] * 100)
ajp_port = 8009 + (node['lumis']['lumisportal']['ports'] * 100)
ssl_port = 8443 + (node['lumis']['lumisportal']['ports'] * 100)

template "/etc/tomcat6/server.xml" do
  source "server.xml.erb"
  mode "0644"
  owner "root"
  group "root"
  variables(
    :shutdown_port => shutdown_port,
    :http_port => http_port,
    :ajp_port => ajp_port,
    :ssl_port => ssl_port,
    :http_attr => node['lumis']['tomcat6']['http_attr'],
    :ajp_attr => node['lumis']['tomcat6']['ajp_attr'],
    :jvm_route => node['fqdn']
  )
  notifies :restart, resources(:service => "tomcat6")
end

link "/usr/share/tomcat6/conf/Catalina/localhost/ROOT.xml" do
  to "#{node['lumis']['lumisportal']['home']}/setup/Tomcat/ROOT.xml"
end

execute "Restart Tomcat 6" do
  command "/sbin/service tomcat6 restart"
  not_if "test -d #{node['lumis']['lumisportal']['dir']}/lumisdata/log"
end
